﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Common;
using Common.Views;
using ElectrosLtdApplication.Models;
using System.Drawing;
using System.Drawing.Imaging;

namespace ElectrosLtdApplication.Controllers
{
    public class ManageReturnsController : MenusController
    {
        //
        // GET: /ManageReturns/

        [Authorize]
        public ActionResult Index()
        {
            List<OrdersView> products = new OrdersServ.OrdersServiceClient().GetProductsBoughtByUser(HttpContext.User.Identity.Name).ToList();
            return View("Index", products);
        }

        public FileContentResult GetBarcode(string rand)
        {
            var client = new BarcodeServ.BarCodeSoapClient("BarCodeSoap");
            byte[] barcode = client.Code39(rand, 6, true, null);
            if (barcode != null)
            {
                return new FileContentResult(barcode, "image/jpeg");
            }
            else
                return null;
        }

        [Authorize]
        public ActionResult FaultDetails(int pId, Guid oId)
        {
            string randomNum = new RandomNumServ.RandomNumberGeneratorClient().GenerateRandomNumber();
            ManageReturnsModel rm = new ManageReturnsModel(HttpContext.User.Identity.Name);

            try
            {
                rm.ProductID = pId;
                rm.OrderID = oId;
                rm.RandomNumber = randomNum;

                List<OrdersView> products = rm.Products;

                List<SelectListItem> faultsList = new List<SelectListItem>();
                IList<FaultCategory> faults = new FaultsServ.FaultsServiceClient().GetFaultCategories();
                foreach (FaultCategory f in faults)
                    faultsList.Add(new SelectListItem { Text = f.Category, Value = f.FaultyCategoryID.ToString() });

                ViewData["faultList"] = faultsList;
                ViewData["randomNum"] = randomNum.ToString();
                ViewData["pId"] = pId;
                ViewData["oId"] = oId;
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", "Fault Details must be reported.");
            }
            return View("FaultDetails", rm);
        }

        [HttpPost]
        public ActionResult FaultDetails(string randomNumber, int FaultCategory, Guid OrderID, int ProductID, string FaultDetails)
        {
            try
            {
                string username = HttpContext.User.Identity.Name;
                var client = new BarcodeServ.BarCodeSoapClient("BarCodeSoap");
                byte[] barcode = client.Code39(randomNumber, 6, true, null);
                Guid faultID = Guid.NewGuid();
                new FaultsServ.FaultsServiceClient().AddFault(faultID, FaultCategory, OrderID, ProductID, FaultDetails, barcode, username);

                return RedirectToAction("Index", "Home");
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", "Fault Details must be reported.");
                return View();
            }
        }
    }
}
